/** 
 * 材质编辑工具 
 * */
import { Space } from 'antd';
import styles from './index.module.less';
import React from 'react';
import * as THREE from 'three';
import Controller from './Controller';
import useReceiveProps from './Controller/hooks/useReceiveProps';
import SelectedProperties from './panels/SelectedProperties';

const EditGeometryContext = React.createContext<Controller>(null as any);

export interface IEditGeometryProps {

    /** 当前编辑的图形 */
    geometry?: THREE.BufferGeometry;

    /** 材质改变了 */
    onChange?: (e: THREE.BufferGeometry) => void;
}

export default class EditGeometry extends React.Component<IEditGeometryProps>{

    /** 获取控制器沟子 */
    static useController = () => React.useContext(EditGeometryContext);

    render() {
        return <Main {...this.props} />
    }
}

function Main(props: IEditGeometryProps) {

    const controller = React.useMemo(() => new Controller(), []);

    useReceiveProps(controller, props.geometry, props.onChange);

    return (
        <EditGeometryContext.Provider value={controller}>
            <Space direction="vertical" className={styles.editGeometry}>
                <SelectedProperties />
            </Space>
        </EditGeometryContext.Provider>
    )
}